<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'edit';
$return=array();
if($_RQ['op']=='edit'){
    $return['coupon']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0),array('id','title'));
	$coupons=pdo_getall('jy_weishop_wxcard',array('uniacid'=>$_SESSION['uniacid'],'card_type'=>'CASH'),array('id','title'));
	foreach($coupons as $v){
		$v['title']=$v['title'].'(微信券)';
		$v['id'] .='w';
		$return['coupon'][]=$v;
	}
	$return['status']=array('1'=>'上架','0'=>'下架');
	$return['type']=array('1'=>'选择会员','2'=>'全部发送');
	$return['item']=$this->plugin_setting('sendcoupon');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'优惠券设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'发送设置',
				'list'=>array(					
					array('type'=>'select','name'=>'coupon','title'=>'选择优惠券','options'=>'coupon'),			
					array('type'=>'string','name'=>'num','title'=>'每人发放数量','help'=>'此处受总数限制，如果剩余张数不足以发放给选定会员数量，则无法发放'),
					array('type'=>'radio','name'=>'type','title'=>'发送用户','options'=>'type'),
					array('type'=>'switch','name'=>'iswxapp','title'=>'属于小程序用户？','help'=>'小程序用户,不推送模板消息！'),
					array('type'=>'text','name'=>'mids','title'=>'选择用户','show'=>array('type'=>1),'help'=>'填写会员id，以英文逗号分隔 如 1,2'),
				)
			),	
			array(
				'title'=>'模板消息设置',
				'list'=>array(
					array('type'=>'string','name'=>'templateid','title'=>'模板id','help'=>'模版搜索关键字：任务处理通知（<red>创建两个文本字段的模板！</red>）'),
					array('type'=>'string','name'=>'value1','title'=>'通知信息1','help'=>'例如：恭喜您获得优惠卷'),
					array('type'=>'string','name'=>'name1','title'=>'通知信息1字段','help'=>'例如：thing2，要跟上面创建的模板-里面的字段一致'),
					array('type'=>'string','name'=>'value2','title'=>'通知信息2','help'=>'例如：请您点击查看'),
					array('type'=>'string','name'=>'name2','title'=>'通知信息2字段','help'=>'例如：thing3，要跟上面创建的模板-里面的字段一致'),				
				)
			),
	)
	);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$data=get_postdata();
	$num=intval($data['num']);
	if($num>20){
		//exi('每人最多发放20张！','error');
	}
	if(!$num){
		exi('请输入发送数量！','error');
	}
	if(!$data['coupon']){
		exi('请选择优惠券！','error');
	}
	if(!$data['name1'] || !$data['name2']){
		exi('请填写通知信息1字段，通知信息2字段！','error');
	}
	$sendcoupon=array(
		'templateid'=>$data['templateid'],
		'value1'=>$data['value1'],
		'name1'=>$data['name1'],
		'value2'=>$data['value2'],
		'name2'=>$data['name2'],
	);
	$this->plugin_setting('sendcoupon',$sendcoupon);
	$msg=array( 
		$data['name1']=>array('value'=>$data['value1'],'name'=>$data['name1'],'title'=>'通知信息1'),
		$data['name2']=>array('value'=>$data['value2'],'name'=>$data['name2'],'title'=>'通知信息2'), 
	);
	if($data['type']=1){
		$member=explode(',',$data['mids']);
	}else{
		$user=pdo_getall('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'onemid'=>0,'is_virtual'=>0),array('mid'));
		$member=array();
		foreach($user as $v){
			$member[]=$v['mid'];	
		}
	}
	if(!count($member)){
		exi('请输入发送用户！','error');
	}
	foreach($member as $m){
        if(!$m){
            continue;
        }
		if(substr($data['coupon'],-1)=='w'){
			exi('发放微信卡券失败！','error');
			$couponid=substr($data['coupon'],0,-1);
			$data2=array('mid'=>$m,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$couponid,'remark'=>'手动方法','createtime'=>TIMESTAMP);
			for($i=1;$i<=$num;$i++){
				pdo_insert('jy_weishop_wxcard_getlog',$data2);
			}
		}else{
			$data1=array('mid'=>$m,'status'=>0,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$data['coupon'],'getfrom'=>0,'createtime'=>TIMESTAMP);
			$data2=array('mid'=>$m,'uniacid'=>$_SESSION['uniacid'],'couponid'=>$data['coupon'],'gettype'=>1,'createtime'=>TIMESTAMP);
			for($i=1;$i<=$num;$i++){
				pdo_update('jy_weishop_coupon',array('last +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$data['coupon']));
				pdo_insert('jy_weishop_coupon_getlog',$data2);
				pdo_insert('jy_weishop_coupon_log',$data1);
			}
		}
    }
    foreach($member as $m){
        if(!$m){
            continue;
        }
        $url=custom_url('jy_weishop/coupon.myindex/',true);
        if(!$data['iswxapp'] && $data['templateid']){
            cfc('account')->sendTplNotice($m, $data['templateid'], $msg, $url,true);
        }
    }
	exi('发放成功','');
}
?>